#!/bin/bash
# chkconfig: 2345 80 20
. /opt/reyzar/conf/socs.conf
num_saIP4=${#saIP4[@]}
num_saIP6=${#saIP6[@]}
str_saIP4=""
str_saIP6=""
if [ -z $csDomain ]; then
	csDomain=reyzar.com
fi

if [ -z $hostname ]; then
        hostname=`hostname`
fi

if [ -z $tmHello ]; then
	tmHello=3
fi

if [ -z $tmDead ]; then
	tmDead=9
fi

if [ -z $tmStable ]; then
	tmStable=9
fi


if [ -z $mcIP4 ]; then
	mcIP4=239.0.0.1
fi

if [ -z $mcIP6 ]; then
	mcIP6=FF12::1
fi


case $1 in
	start)
	if [ $num_saIP4 -gt 0 ]; then
		echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
		echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
		echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
		echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
		echo "2" > /proc/sys/net/ipv4/conf/all/rp_filter
		index=0
		i=0
		while [ $index  -lt $num_saIP4 ]; do
			/sbin/ifconfig lo:$index ${saIP4[$index]} netmask 255.255.255.255  up
			str_saIP4=${str_saIP4}${saIP4[$index]}
			index=$((index+1))
			if [ $index != $num_saIP4 ]; then
				str_saIP4=${str_saIP4}","
			fi
		done	
	fi;
	if [ $num_saIP6 -gt 0 ]; then
	    index=0
        while [ $index  -lt $num_saIP6 ]; do
			str_saIP6=${str_saIP6}${saIP6[$index]}

			str1=${saIP6[$index]}"/128"
            str2=${str1#*/}
            str3=${str2%%/*}
            if [ ${str3} == "128" ]; then
                /sbin/ifconfig lo inet6 add ${saIP6[$index]}/128
            fi  

			#/sbin/ifconfig lo inet6 add ${saIP6[$index]}/128
        	index=$((index+1))
        	if [ $index != $num_saIP6 ]; then
        	        str_saIP6=${str_saIP6}","
        	fi
	    done
		array=`ifconfig -a |grep eth |awk '{print $1}'`
		array_ifname=($array)
		num_ifname=${#array_ifname[@]}
		if [ $num_ifname -gt 0 ]; then
		        index=0
		        while [ $index -lt $num_ifname ]; do
				ifconfig ${array_ifname[$index]} allmulti
		                index=$((index+1))
		        done
		fi
	fi;
	if [ $num_saIP4 -gt 0 -a $num_saIP6 -gt 0 ]; then
		modprobe socs saIP4=$str_saIP4 saIP6=$str_saIP6 hostname=$hostname tmHello=$tmHello tmDead=$tmDead tmStable=$tmStable mcIP4=$mcIP4 csDomain=$csDomain
	elif [ $num_saIP4 -gt 0 ]; then
		modprobe socs saIP4=$str_saIP4 hostname=$hostname tmHello=$tmHello tmDead=$tmDead tmStable=$tmStable mcIP4=$mcIP4 csDomain=$csDomain
	elif [ $num_saIP6 -gt 0 ]; then
		modprobe socs saIP6=$str_saIP6 hostname=$hostname tmHello=$tmHello tmDead=$tmDead tmStable=$tmStable mcIP4=$mcIP4 csDomain=$csDomain
	else
		modprobe socs  hostname=$hostname tmHello=$tmHello tmDead=$tmDead tmStable=$tmStable mcIP4=$mcIP4 csDomain=$csDomain
	fi
	grep $mcIP4 /etc/sysconfig/iptables > /dev/null
	if [ $? -ne 0 ]; then
    		iptables -I INPUT -p udp -d $mcIP4  -j ACCEPT
		/etc/init.d/iptables save > /dev/null
	fi
	echo "Start SOCS Server!"
	;;


	stop)
	if [ $num_saIP4 -gt 0 ]; then
		i=0
		while [ $i -lt $num_saIP4 ]; do
			/sbin/ifconfig lo:$i down
			i=$((i+1))
		done
	
		echo "0" > /proc/sys/net/ipv4/conf/lo/arp_ignore
		echo "0" > /proc/sys/net/ipv4/conf/lo/arp_announce
		echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore
		echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
		echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter
	fi
	
	if [ $num_saIP6 -gt 0 ]; then
	        index=0
            while [ $index  -lt $num_saIP6 ]; do
				str1=${saIP6[$index]}"/128"
                str2=${str1#*/}
                str3=${str2%%/*}
                if [ ${str3} == "128" ]; then
                    /sbin/ifconfig lo inet6 del ${saIP6[$index]}/128
                fi  
                index=$((index+1))
            done
            array=`ifconfig -a |grep eth |awk '{print $1}'`
            array_ifname=($array)
            num_ifname=${#array_ifname[@]}
            if [ $num_ifname -gt 0 ]; then
                    index=0
                    while [ $index -lt $num_ifname ]; do
                            ifconfig ${array_ifname[$index]} -allmulti
                            index=$((index+1))
                    done
            fi
	fi
	rmmod socs
	grep $mcIP4 /etc/sysconfig/iptables > /dev/null
	if [ $? -eq 0 ]; then
    		iptables -D INPUT -p udp -d $mcIP4  -j ACCEPT 
		/etc/init.d/iptables save > /dev/null
	fi
	echo "Stop SOCS Server!"

	;;


	*)
	echo "Usage:`basename $0` (start|stop)"
esac
